/*
 * @Description: 你的代码我的心
 * @Author: GGB
 * @Date: 2022-01-17 14:48:51
 * @LastEditors: GGB
 * @LastEditTime: 2022-01-20 17:52:15
 */
import { app, BrowserWindow, screen,Menu } from 'electron';
import is_dev from 'electron-is-dev';
import { join } from 'path';

const isMac = process.platform === 'darwin'

let mainWindow: BrowserWindow | null = null;
// 移除对应的报错
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

class createWin {
  constructor() {
    const displayWorkAreaSize = screen.getAllDisplays()[0].workArea;
    mainWindow = new BrowserWindow({
      width: parseInt(`${displayWorkAreaSize.width * 0.85}`, 10),
      height: parseInt(`${displayWorkAreaSize.height * 0.85}`, 10),
      movable: true,
      // frame: false,
      show: false,
      center: true,
      resizable: true,
      // transparent: true,
      titleBarStyle: 'default',
      webPreferences: {
        devTools: true,
        contextIsolation: false,
        nodeIntegration: true,
        enableRemoteModule: true,
      },
      backgroundColor: '#fff',
    });
    const URL = is_dev
      ? `http://localhost:${process.env.PORT}` // vite 启动的服务器地址
      : `file://${join(__dirname, '../index.html')}`; // vite 构建后的静态文件地址

    mainWindow.loadURL(URL);

    mainWindow.on('ready-to-show', () => {
      mainWindow.show();
    });
  }
}

// 设置音频 自动播放
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');

// 设置菜单
const template = [
  // { role: 'appMenu' }
  ...(isMac ? [{
    label: app.name,
    submenu: [
      { role: 'about',label:'关于' },
      { type: 'separator',label:'' },
      { role: 'services',label:'服务' },
      { role: 'hide',label:'隐藏' },
      { role: 'hideOthers' ,lable:'隐藏其他'},
      { role: 'unhide',label:'显示' },
      { type: 'separator' },
      { role: 'quit',label:'退出' }
    ]
  }] : []),
  // { role: 'fileMenu' }
  {
    label: '应用',
    submenu: [
      isMac ? { role: 'close',label:'关闭' } : { role: 'quit',label:'退出' }
    ]
  },
  // { role: 'editMenu' }
  {
    label: '编辑',
    submenu: [
      { role: 'undo',lable:'撤消' },
      { role: 'redo',label:'重做' },
      { type: 'separator' },
      { role: 'cut',label:'剪切' },
      { role: 'copy',label:'复制' },
      { role: 'paste',label:'粘贴' },
      ...(isMac ? [
        { role: 'pasteAndMatchStyle',label:'' },
        { role: 'delete',label:'删除' },
        { role: 'selectAll',label:'全选' },
        { type: 'separator' },
        {
          label: '语音',
          submenu: [
            { role: 'startSpeaking',lable:'开始' },
            { role: 'stopSpeaking',label:'停止' }
          ]
        }
      ] : [
        { role: 'delete',label:'删除' },
        { type: 'separator' ,label:''},
        { role: 'selectAll' ,label:'全选'}
      ])
    ]
  },
  // { role: 'viewMenu' }
  {
    label: '视图',
    submenu: [
      { role: 'reload',label:'刷新' },
      { role: 'forceReload',label:'强制刷新' },
      { role: 'toggleDevTools',label:'调试' },
      { type: 'separator' },
      { role: 'resetZoom',label:'重置大小' },
      { role: 'zoomIn',label:'缩小', },
      { role: 'zoomOut',label:'放大' },
      { type: 'separator' },
      { role: 'togglefullscreen',label:'全屏' }
    ]
  },
  // { role: 'windowMenu' }
  {
    label: '帮助',
    submenu: [
      { role: 'minimize',label:'迷你视图' },
      { role: 'zoom',label:'自适应' },
      ...(isMac ? [
        { type: 'separator' },
        { role: 'front',label:'字体' },
        { type: 'separator',label:'' },
        { role: 'window',label:'窗口' }
      ] : [
        { role: 'close',label:'退出' }
      ])
    ]
  },
  {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click: async () => {
          const { shell } = require('electron')
          await shell.openExternal('https://electronjs.org')
        }
      }
    ]
  }
]  
// 设置菜单
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)

app.whenReady().then(() => new createWin());

const isFirstInstance = app.requestSingleInstanceLock();

if (!isFirstInstance) {
  app.quit();
} else {
  app.on('second-instance', () => {
    if (mainWindow) {
      mainWindow.focus();
    }
  });
}

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    new createWin();
  }
});
